import { http } from '@/request/http'
import type { LabelVO } from '@/request/api/label'
import { initLabelVO } from '@/request/api/label'
import type { PageDTO, PageVO } from '@/type/type'
import type { MakeOptional } from '@/type'

type BaseResources = {
  // 自增主键
  id: number
  // 资源文件名
  name: string
  // 链接
  url: string
  // 备注
  remark: string
  // 是否启用
  status: boolean
}

export type ResourcesDTO = MakeOptional<BaseResources> & {
  // 类别ID
  labelId: number | undefined
}

export type ResourcesVO = BaseResources & {
  // 资源大小 kb
  size: number
  // 创建时间
  createTime: Date
  // 标签
  label: LabelVO
}

export const initResourcesDTO = (): ResourcesDTO => {
  return {
    id: undefined,
    labelId: undefined,
    name: undefined,
    url: undefined,
    remark: undefined,
    status: undefined
  }
}

export const initResourcesVO = (): ResourcesVO => {
  return {
    id: 0,
    name: '',
    url: '',
    size: 0,
    remark: '',
    createTime: new Date(),
    status: true,
    label: initLabelVO()
  }
}

export const resourcesVO2DTO = (vo: ResourcesVO): ResourcesDTO => {
  return {
    id: vo.id,
    labelId: vo.label.id,
    name: vo.name,
    url: vo.url,
    remark: vo.remark,
    status: vo.status
  }
}

export const getResourcesPageApi = (page: PageDTO | any) => {
  return http<PageVO<ResourcesVO>>({
    url: `/resources/page`,
    method: 'get',
    params: {
      dto: JSON.stringify(page)
    }
  })
}

export const getResourcesAdminPageApi = (page: PageDTO | any) => {
  return http<PageVO<ResourcesVO>>({
    url: `/admin/resources/page`,
    method: 'get',
    params: {
      dto: JSON.stringify(page)
    }
  })
}

export function addResourcesApi(data: FormData) {
  return http<boolean>({
    url: '/admin/resources/insert',
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data: data
  })
}

// 修改
export function updateResourcesApi(data: ResourcesDTO) {
  return http<boolean>({
    url: '/admin/resources/update',
    method: 'PUT',
    data: data
  })
}

// 删除
export function delResourcesApi(ids: number[]) {
  return http<boolean>({
    url: '/admin/resources/delete',
    method: 'DELETE',
    data: ids
  })
}

export function getResourcesLabelListApi() {
  return http<LabelVO[]>({
    url: '/admin/resources/label/list',
    method: 'get'
  })
}
